<template>
  <div class="border-line card">
    <header-title title="" @fullScreen="handleFullScreen"></header-title>
    <div class="subtext">保租房简介</div>
    <div class="subtext" style="margin-left: 1%; width: 49%">项目筹集方式</div>
    <div class="content">
      <div class="column">
        <div style="color: #61a8ff; margin-top: 10px">
          <p style="text-indent: 2em">
            保障性租赁住房，是指政府提供政策支持，多主体投资建设，面向符合条件的新市民、青年人等阶段性住房困难群体，限定租赁用途、租金标准的保障性住房。
          </p>
          <p style="text-indent: 2em">
            坚持以人民为中心，突出住房的民生属性，扩大保障性租赁住房供给，健全完善多主体供给、多渠道保障、租购并举的住房制度，保持房地产市场平稳健康发展，解决新市民、青年人等群体的住房困难问题，促进实现全体市民住有所居。
          </p>
          <p style="text-indent: 2em">
            <span>按照《市人民政府办公厅关于加快发展保障性租赁住房的实施意见》，计划到</span>
            <span style="font-weight: bold; font-size: 20px">2025</span>
            <span>年底，贵阳贵安筹建保障性租赁住房</span>
            <span style="font-weight: bold; font-size: 20px">10</span>
            <span>万套（间），目前，</span>
            <span style="font-weight: bold; font-size: 20px">2021</span>
            <span>年启动建设</span>
            <span style="font-weight: bold; font-size: 20px">3452</span>
            <span>套（间）、</span>
            <span style="font-weight: bold; font-size: 20px">2022</span>
            <span>年启动建设</span>
            <span style="font-weight: bold; font-size: 20px">32871</span>
            <span>套（间）、</span>
            <span style="font-weight: bold; font-size: 20px">2023</span>
            <span>年启动建设</span>
            <span style="font-weight: bold; font-size: 20px">11597</span>
            <span>套（间）、</span>
            <span style="font-weight: bold; font-size: 20px">2024</span>
            <span>年启动建设</span>
            <span style="font-weight: bold; font-size: 20px">17515</span>
            <span>套（间），截至</span>
            <span style="font-weight: bold; font-size: 20px">2024</span>
            <span>年底共启动建设</span>
            <span style="font-weight: bold; font-size: 20px">65435</span>
            <span>套（间）。</span>
          </p>
        </div>
      </div>
      <div class="column" style="margin-left: 1.5%">
        <echarts-view ref="echarts" :options="wayOption" height="300px"></echarts-view>
      </div>
    </div>
  </div>
</template>

<script>
import headerTitle from './headerTitle.vue'
import { echartsView } from '@/components'
import axios from 'axios'
import 'echarts-gl'
export default {
  name: 'Left1',
  props: {
    areaID: {
      type: String,
      default: '',
    },
  },
  watch: {
    areaID: function () {
      this.fetchData()
    },
  },
  components: {
    headerTitle,
    echartsView,
  },
  data() {
    return {
      wayOption: {},
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      // 本地ip
      const echart1 = axios({
        method: 'get',
        timeout: 180000,
        url: process.env.VUE_APP_API_BASE_URL_BZF + '/api/Statistics/TJRY_XMCJFS',
        params: {
          Userid: 1,
          QY: this.areaID,
        },
      })
      Promise.all([echart1]).then((res) => {
        res[0].data.forEach((e) => {
          e.value = Number(e.value)
        })
        this.initWayOptionChart(res[0].data)
      })
    },
    // 生成扇形的曲面参数方程，用于 series-surface.parametricEquation
    getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, height) {
      // 计算
      const midRatio = (startRatio + endRatio) / 2

      const startRadian = startRatio * Math.PI * 2
      const endRadian = endRatio * Math.PI * 2
      const midRadian = midRatio * Math.PI * 2

      // 如果只有一个扇形，则不实现选中效果。
      if (startRatio === 0 && endRatio === 1) {
        isSelected = false
      }

      // 通过扇形内径/外径的值，换算出辅助参数 k（默认值 1/3）
      k = typeof k !== 'undefined' ? k : 1 / 3

      // 计算选中效果分别在 x 轴、y 轴方向上的位移（未选中，则位移均为 0）
      const offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0
      const offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0

      // 计算高亮效果的放大比例（未高亮，则比例为 1）
      const hoverRate = isHovered ? 1.05 : 1

      // 返回曲面参数方程
      return {
        u: {
          min: -Math.PI,
          max: Math.PI * 3,
          step: Math.PI / 32,
        },

        v: {
          min: 0,
          max: Math.PI * 2,
          step: Math.PI / 20,
        },

        x: function (u, v) {
          if (u < startRadian) {
            return offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate
          }
          if (u > endRadian) {
            return offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate
          }
          return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate
        },

        y: function (u, v) {
          if (u < startRadian) {
            return offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate
          }
          if (u > endRadian) {
            return offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate
          }
          return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate
        },

        z: function (u, v) {
          if (u < -Math.PI * 0.5) {
            return Math.sin(u)
          }
          if (u > Math.PI * 2.5) {
            return Math.sin(u)
          }
          return Math.sin(v) > 0 ? 1 * height : -1
        },
      }
    },
    // 生成模拟 3D 饼图的配置项
    getPie3D(pieData, internalDiameterRatio) {
      const series = []
      let sumValue = 0
      let startValue = 0
      let endValue = 0
      const legendData = []
      const k =
        typeof internalDiameterRatio !== 'undefined' ? (1 - internalDiameterRatio) / (1 + internalDiameterRatio) : 1 / 3

      // 为每一个饼图数据，生成一个 series-surface 配置
      for (let i = 0; i < pieData.length; i++) {
        sumValue += pieData[i].value

        const seriesItem = {
          name: typeof pieData[i].name === 'undefined' ? `series${i}` : pieData[i].name,
          type: 'surface',
          parametric: true,
          wireframe: {
            show: false,
          },
          pieData: pieData[i],
          pieStatus: {
            selected: false,
            hovered: false,
            k: k,
          },
        }

        if (typeof pieData[i].itemStyle !== 'undefined') {
          const itemStyle = {}

          // eslint-disable-next-line no-unused-expressions
          typeof pieData[i].itemStyle.color !== 'undefined' ? (itemStyle.color = pieData[i].itemStyle.color) : null
          // eslint-disable-next-line no-unused-expressions
          typeof pieData[i].itemStyle.opacity !== 'undefined'
            ? (itemStyle.opacity = pieData[i].itemStyle.opacity)
            : null

          seriesItem.itemStyle = itemStyle
        }
        series.push(seriesItem)
      }

      // 使用上一次遍历时，计算出的数据和 sumValue，调用 getParametricEquation 函数，
      // 向每个 series-surface 传入不同的参数方程 series-surface.parametricEquation，也就是实现每一个扇形。
      for (let i = 0; i < series.length; i++) {
        endValue = startValue + series[i].pieData.value
        series[i].pieData.startRatio = startValue / sumValue
        series[i].pieData.endRatio = endValue / sumValue
        series[i].parametricEquation = this.getParametricEquation(
          series[i].pieData.startRatio,
          series[i].pieData.endRatio,
          false,
          false,
          k,
          series[i].pieData.value * 2
        )

        startValue = endValue

        legendData.push(series[i].name)
      }

      // // 补充一个透明的圆环，用于支撑高亮功能的近似实现。
      series.push({
        name: 'mouseoutSeries',
        type: 'surface',
        parametric: true,
        wireframe: {
          show: false,
        },
        itemStyle: {
          opacity: 0.1,
          color: '#E1E8EC',
        },
        parametricEquation: {
          u: {
            min: 0,
            max: Math.PI * 2,
            step: Math.PI / 20,
          },
          v: {
            min: 0,
            max: Math.PI,
            step: Math.PI / 20,
          },
          x: function (u, v) {
            return ((Math.sin(v) * Math.sin(u) + Math.sin(u)) / Math.PI) * 2
          },
          y: function (u, v) {
            return ((Math.sin(v) * Math.cos(u) + Math.cos(u)) / Math.PI) * 2
          },
          z: function (u, v) {
            return Math.cos(v) > 0 ? -0.5 : -5
          },
        },
      })

      // // 补充一个透明的圆环，用于支撑高亮功能的近似实现。
      series.push({
        name: 'mouseoutSeries',
        type: 'surface',
        parametric: true,
        wireframe: {
          show: false,
        },
        itemStyle: {
          opacity: 0.1,
          color: '#E1E8EC',
        },
        parametricEquation: {
          u: {
            min: 0,
            max: Math.PI * 2,
            step: Math.PI / 20,
          },
          v: {
            min: 0,
            max: Math.PI,
            step: Math.PI / 20,
          },
          x: function (u, v) {
            return ((Math.sin(v) * Math.sin(u) + Math.sin(u)) / Math.PI) * 2
          },
          y: function (u, v) {
            return ((Math.sin(v) * Math.cos(u) + Math.cos(u)) / Math.PI) * 2
          },
          z: function (u, v) {
            return Math.cos(v) > 0 ? -5 : -7
          },
        },
      })
      series.push({
        name: 'mouseoutSeries',
        type: 'surface',
        parametric: true,
        wireframe: {
          show: false,
        },
        itemStyle: {
          opacity: 0.1,
          color: '#E1E8EC',
        },

        parametricEquation: {
          u: {
            min: 0,
            max: Math.PI * 2,
            step: Math.PI / 20,
          },
          v: {
            min: 0,
            max: Math.PI,
            step: Math.PI / 20,
          },
          x: function (u, v) {
            return ((Math.sin(v) * Math.sin(u) + Math.sin(u)) / Math.PI) * 2.2
          },
          y: function (u, v) {
            return ((Math.sin(v) * Math.cos(u) + Math.cos(u)) / Math.PI) * 2.2
          },
          z: function (u, v) {
            return Math.cos(v) > 0 ? -7 : -7
          },
        },
      })
      return series
    },
    initWayOptionChart(res) {
      let total = 0
      if (total === 0) {
        res.forEach((item) => {
          total += item.value
        })
      }
      res.forEach((item) => {
        item.value = Number(((item.value / total) * 100).toFixed())
      })
      const series = this.getPie3D(res, 0.8, 240, 28, 26, 0.5)
      series.push({
        name: 'pie2d',
        type: 'pie',
        label: {
          color: '#fff',
          opacity: 1,
          fontSize: 20,
          lineHeight: 20,
          textStyle: {
            fontSize: 20,
          },
          textBorderColor: 'transparent',
        },
        labelLine: {
          length: 40,
          length2: 80,
        },
        startAngle: -25, // 起始角度，支持范围[0, 360]。
        clockwise: false, // 饼图的扇区是否是顺时针排布。上述这两项配置主要是为了对齐3d的样式
        radius: ['20%', '50%'],

        center: ['50%', '50%'],
        data: res,
        itemStyle: {
          opacity: 0,
        },
      })
      // 准备待返回的配置项，把准备好的 legendData、series 传入。
      const option = {
        animation: true,
        tooltip: {
          formatter: (params) => {
            if (params.seriesName !== 'mouseoutSeries' && params.seriesName !== 'pie2d') {
              return `${
                params.seriesName
              }<br/><span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${
                params.color
              };"></span>${res[params.seriesIndex].value + '%'}`
            }
          },
        },
        labelLine: {
          show: true,
          lineStyle: {
            color: '#7BC0CB',
          },
        },
        label: {
          show: true,
          position: 'outside',
          formatter: '{b} \n{c}%',
        },
        xAxis3D: {
          min: -1,
          max: 1,
        },
        yAxis3D: {
          min: -1,
          max: 1,
        },
        zAxis3D: {
          min: -1,
          max: 1,
        },
        grid3D: {
          show: false,
          boxHeight: 0.7,
          bottom: '10%',

          viewControl: {
            distance: 220,
            alpha: 50,
            beta: 35,
            autoRotate: false, // 自动旋转
          },
        },
        series: series,
      }
      this.wayOption = option
    },
    handleFullScreen() {
      this.$emit('fullScreen')
    },
  },
}
</script>
<style lang="less" scoped>
.card {
  height: 400px;
  font-size: 18px;
  .content {
    display: flex;
    flex-direction: row;
    margin-top: 6px;
    .column {
      flex: 1;
      &:first-child {
        overflow-y: auto;
        height: 300px;
        &::-webkit-scrollbar {
          display: none;
        }
      }
    }
  }
}
.subtext {
  color: #fff;
  background-color: #0d5b78;
  display: inline-block;
  width: 50%;
  font-size: 20px;
  box-sizing: border-box;
  padding: 5px 10px;
  margin: 6px 0;
}
</style>
